package gq.example.json;

import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderProvider;
import com.alibaba.fastjson2.writer.ObjectWriter;
import com.alibaba.fastjson2.writer.ObjectWriterProvider;

import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class FastjsonConfigurator {
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    static {
        registerSerializers();
        registerDeserializers();
    }

    public static void init() {
        // 确保类加载时初始化
    }

    private static void registerSerializers() {
        ObjectWriterProvider writerProvider = JSONFactory.getDefaultObjectWriterProvider();

        // 注册 LocalDateTime 序列化
        ObjectWriter<LocalDateTime> dateTimeWriter = (jsonWriter, object, fieldName, fieldType, features) -> {
            if (object == null) {
                jsonWriter.writeNull();
                return;
            }
            LocalDateTime dateTime = (LocalDateTime) object; // 添加类型转换
            jsonWriter.writeString(dateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        };
        writerProvider.register(LocalDateTime.class, dateTimeWriter);

        // 注册 LocalDate 序列化
        ObjectWriter<LocalDate> dateWriter = (jsonWriter, object, fieldName, fieldType, features) -> {
            if (object == null) {
                jsonWriter.writeNull();
                return;
            }
            LocalDate date = (LocalDate) object; // 添加类型转换
            jsonWriter.writeString(date.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        };
        writerProvider.register(LocalDate.class, dateWriter);

        // 注册 LocalTime 序列化
        ObjectWriter<LocalTime> timeWriter = (jsonWriter, object, fieldName, fieldType, features) -> {
            if (object == null) {
                jsonWriter.writeNull();
                return;
            }
            LocalTime time = (LocalTime) object; // 添加类型转换
            jsonWriter.writeString(time.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        };
        writerProvider.register(LocalTime.class, timeWriter);
    }

    private static void registerDeserializers() {
        ObjectReaderProvider readerProvider = JSONFactory.getDefaultObjectReaderProvider();

        // 注册 LocalDateTime 反序列化
        ObjectReader<LocalDateTime> dateTimeReader = (jsonReader, fieldType, fieldName, features) -> {
            String str = jsonReader.readString();
            if (str == null || str.isEmpty()) {
                return null;
            }
            return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT));
        };
        readerProvider.register(LocalDateTime.class, dateTimeReader);

        // 注册 LocalDate 反序列化
        ObjectReader<LocalDate> dateReader = (jsonReader, fieldType, fieldName, features) -> {
            String str = jsonReader.readString();
            if (str == null || str.isEmpty()) {
                return null;
            }
            return LocalDate.parse(str, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
        };
        readerProvider.register(LocalDate.class, dateReader);

        // 注册 LocalTime 反序列化
        ObjectReader<LocalTime> timeReader = (jsonReader, fieldType, fieldName, features) -> {
            String str = jsonReader.readString();
            if (str == null || str.isEmpty()) {
                return null;
            }
            return LocalTime.parse(str, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT));
        };
        readerProvider.register(LocalTime.class, timeReader);
    }
}